<template>
  <div>
    <van-list :error.sync="error" error-text="请求失败，点击重新加载" v-model="loading" :finished="finished" finished-text="没有更多了"
      @load="onLoad">
      <!--  <van-cell v-for="item in list" :key="item.com_id+''" :title="item.content" /> -->
      <CommentItem @replayComment="$emit('replayComment',$event)" :comments="item" v-for="item in list"
        :key="item.com_id+''" />
    </van-list>
  </div>
</template>

<script>
import CommentItem from '@/components/comments-item.vue'
import { getArticleComment } from '@/api/comments.js'
export default {
  name: 'Comment',
  data () {
    return {
      // list: [],
      loading: false,
      finished: false,
      offset: null,
      limit: 10, // 每页条数
      total: 0,
      error: false
    }
  },
  props: {
    // 评论类型，a-对文章(article)的评论，c-对评论(comment)的回复
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Object, Number],
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },
  methods: {
    async onLoad () {
      try {
        // 1.发送请求获取数据
        const res = await getArticleComment({
          type: this.type,
          source: this.source + '',
          offset: this.offset,
          limit: this.limit
        })
        // 2.保存数据
        // console.log(res)
        this.list.push(...res.data.data.results)
        this.total = res.data.data.total_count
        this.$emit('totalCount', this.total)
        // 3.加载状态结束
        this.loading = false
        // 4.数据全部加载完成
        if (this.list.length === this.total) {
          this.finished = true
        } else {
          this.offset = res.data.data.last_id
        }
      } catch (err) {
        this.error = true
        this.$toast('获取评论列表失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    CommentItem
  },
  created () {
    // console.log(this.type)
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
